package com.fuzongle.tank.common.push.mobile;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;


/**
 * 重构--网上找
 * @Description:说明：极光推送工具类-app头部显示消息：http://docs.jiguang.cn/jpush/guideline/push-SMS-intro/
 * @author: fuzongle   
 * @date: 2019年3月15日 下午3:31:19
 */
@Controller
@RequestMapping(value="/jpush")
public class JPushClientUtil {
	private static final String appKey = "ee31547d772f94e67473d32a"; // 必填，例如466f7032ac604e02fb7bda89
	private static final String masterSecret = "b134ed1d0c19baf4f446a10c";// "13ac09b17715bd117163d8a1";//必填，每个应用都对应一个masterSecret

	private static final String appKey2 = "98533f2b4126e4c6f65bf8ab"; //
	private static final String masterSecret2 = "4b4cabbd668686c05a4bc91c";//
	public static final int MAX = Integer.MAX_VALUE;
	public static final int MIN = (int) MAX / 2;
	
	/**
	 * 保存离线的时长。秒为单位。最多支持10天（864000秒）。
	 * 0 表示该消息不保存离线。即：用户在线马上发出，当前不在线用户将不会收到此消息。
	 * 此参数不设置则表示默认，默认为保存1天的离线消息（86400秒)。
	 */
	private static long timeToLive = 60 * 60 * 24;
	public static void main(String[] args) {
		String msgTitle = "推送测试";
		String msgContent = "看到信息了么，看到就推送成功了！";
		String userid="95";
		push("bk_"+userid,msgTitle,"verify");
	}

	/**
	 * 生成极光推送对象PushPayload（采用java SDK）
	 * @param alias
	 * @param alert
	 * @return PushPayload
	 */
	public static PushPayload buildPushObject_android_ios_alias_alert(String alias,String alert,String extra){
		return PushPayload.newBuilder()
				.setPlatform(Platform.android_ios())
				.setAudience(Audience.alias(alias))
				.setNotification(Notification.newBuilder()
						.addPlatformNotification(AndroidNotification.newBuilder()
								.addExtra("type", extra)
								.setAlert(alert)
								.build())
						.addPlatformNotification(IosNotification.newBuilder()
								.addExtra("type", extra)
								.setAlert(alert)
								.build())
						.build())
				.setOptions(Options.newBuilder()
						.setApnsProduction(false)//true-推送生产环境 false-推送开发环境（测试使用参数）
						.setTimeToLive(90)//消息在JPush服务器的失效时间（测试使用参数）
						.build())
				.build();
	}

	/**
	 * 极光推送方法(采用java SDK)
	 * @param alias
	 * @param alert
	 * @return PushResult
	 */
	public static PushResult push(String alias,String alert,String extra){
		JPushClient jpushClient = new JPushClient(masterSecret, appKey,3);
		JPushClient jpushClient2 = new JPushClient(masterSecret2, appKey2,3);
		PushPayload payload = buildPushObject_android_ios_alias_alert(alias,alert,extra);
		try {
			jpushClient2.sendPush(payload);
			return jpushClient.sendPush(payload);
		} catch (APIConnectionException e) {
			e.printStackTrace();
			return null;
		} catch (APIRequestException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 给多个人推送消息
	 * @param list
	 */
	/*public static void pushManyMessage(List<PageData> list){

		for(int i=0;i<list.size();i++){
			pushMessage(
				list.get(i).get("msgTitle").toString(),
				list.get(i).get("msgContent").toString(),
				list.get(i).get("user_id").toString()
			);
		}

	}*/

	/**
	 * 保持 sendNo 的唯一性是有必要的
	 */
	public static int getRandomSendNo() {
		return (int) (MIN + Math.random() * (MAX - MIN));
	}

}
